package com.sky.controller.admin;

import com.sky.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/admin/common")
@Slf4j
public class CommomController {

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws IOException {
        //原始文件命
        String originalFilename = file.getOriginalFilename();
        //截取原始文件名后缀
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        //构造新文件名
        String newFileName = UUID.randomUUID().toString() + extension;

        //将文件按照新的文件命存在本地
        file.transferTo(new java.io.File("D:\\" + newFileName));

        return Result.success("http://localhost:8080/" + newFileName);

    }
}
